package com.icyrain11.training.handler.exception;



import com.icyrain11.training.common.Result;
import com.icyrain11.training.constants.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.bind.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author icyrain11
 * @version 1.8
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获运行时异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return Result.fail(HttpStatus.ERROR, e.getMessage());
    }

    /**
     * TODO
     * 参数校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public Result<?> bindExceptionHandler(BindException e) {
        log.info("出现参数绑定异常 {}", e.getMessage());
        return Result.fail();
    }

}
